package controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import service.EmployeeService;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/admin")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private Result result;


    //登录
    @PostMapping("/login")
    public ModelAndView login(HttpSession session, ModelAndView mv, String employeeid, String password) {
        int flag = employeeService.login(employeeid, password);
        if (flag == 1) {
            session.setAttribute("employeeid", employeeid);
            mv.setViewName("redirect:/admin/userlist");
        }
        if (flag == 2) {
            result.setInfo("对不起，密码错误！");
            mv.setViewName("admin/message");
        }
        if (flag == 3) {
            result.setInfo("对不起，用户名不存在！");
            mv.setViewName("admin/message");
        }
        result.setOk(false);
        result.setHref("javascript:history.back(-1)");
        mv.addObject(result);//将result封装到模型与视图对象mv里
        return mv;
    }

    //注销
    @RequestMapping("/logout")
    public String logout(HttpSession session){
        //把session中的employeeid移除
        session.removeAttribute("employeeid");
        return "/admin/login";
    }



}
